package com.lujianfei.ann.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDesktopPane;
import javax.swing.JPanel;

import com.lujianfei.ann.base.BaseChildWindow;
import com.lujianfei.ann.base.BaseChildWindow.OnCloseListener;
import com.lujianfei.ann.base.BaseMenuWindow;
import com.lujianfei.ann.ui.subui.GaussDistributionFrame;
import com.lujianfei.ann.ui.subui.GradientDescentFrame;
import com.lujianfei.lib.sdautolayout.impl.SDAutoLayout;

public class MainFrame extends BaseMenuWindow {
	
	JDesktopPane desktopPane;
	JPanel jpanel = new JPanel();

	List<BaseChildWindow> childFrames = new ArrayList<>();
	
	public MainFrame() {
		super();
		setTitle("神经网络训练过程");
		jpanel.setBackground(Color.DARK_GRAY);
		SDAutoLayout.forComponent(jpanel).widthIs(getWidth()).heightIs(getHeight());
		desktopPane = new JDesktopPane();
		getContentPane().add(desktopPane, BorderLayout.CENTER);
		
		desktopPane.add(jpanel);
		desktopPane.setLayer(jpanel, -1);
	}
	

	@Override
	protected List<MenuBean> getMenuBean() {
		// TODO Auto-generated method stub
		List<MenuBean> menuBeans = new ArrayList<>();
		List<MenuBean> menuFile = new ArrayList<>();
		menuBeans.add(new MenuBean.Builder().setName("文件").setChilds(menuFile).create());
		menuFile.add(new MenuBean.Builder().setName("退出").setActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				exit();
			}
		}).create());
		List<MenuBean> menuOperation = new ArrayList<>();
		menuBeans.add(new MenuBean.Builder().setName("视图").setChilds(menuOperation).create());
		menuOperation.add(new MenuBean.Builder().setName("梯度下降实现线性回归").setActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				showSubFrame(GradientDescentFrame.create());
			}
		}).create());
		menuOperation.add(new MenuBean.Builder().setName("高斯分布生成").setActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				showSubFrame(GaussDistributionFrame.create());
			}
		}).create());
		return menuBeans;
	}
	
	private void showSubFrame(BaseChildWindow childFrame) {
		desktopPane.add(childFrame);
		try {
			childFrame.setSelected(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		childFrame.setOnCloseListener(new OnCloseListener() {
			
			@Override
			public void onClose(Object frame) {
				// TODO Auto-generated method stub
				childFrames.remove(frame);
			}
		});
		childFrames.add(childFrame);
	}
}
